home *** CD-ROM | disk | FTP | other *** search
- /*--------------------------------------------------------------------------*/
- /* */
- /* */
- /* ------------ Bit-Bucket Software <no-Inc> */
- /* \ 10001101 / Writers and Distributors of */
- /* \ 011110 / No-Cost<no-tm> Software. */
- /* \ 1011 / */
- /* ------ KopyRong (K) 1987. ALL RIGHTS REVERSED. */
- /* */
- /* */
- /* This module was written by Vince Perriello */
- /* */
- /* */
- /* BinkleyTerm Script Handler Module */
- /* */
- /* */
- /* This software package is being distributed WITH FULL SOURCE CODE */
- /* with the following conditions: 1) If anything awful happens */
- /* because you use it (or don't use it), you accept full */
- /* responsibility; 2) you don't start making tons of voice calls to */
- /* the authors to complain or make suggestions about enhancements, */
- /* useful or otherwise; 3) you do not reuse this code in commercial */
- /* products without specific permission to do so from the authors; */
- /* 4) If you find any problems you send fixes to the authors for */
- /* inclusion in updates; 5) You find some way to express your */
- /* appreciation for this method of distribution, either by writing */
- /* code or application notes, or just sending along a "Thank You" */
- /* message. */
- /* */
- /* There is copyrighted code in this product. We either wrote it */
- /* ourselves or got permission to use it. Please don't force us to */
- /* pay a lawyer -- have some respect for our motives and don't abuse */
- /* this "license". */
- /* */
- /* */
- /*--------------------------------------------------------------------------*/
-
- #include <stdio.h>
- #include <signal.h>
- #include <ctype.h>
- #include <conio.h>
- #include "com.h"
- #include "xfer.h"
-
- extern int un_attended;
- extern int fullscreen;
-
- /*--------------------------------------------------------------------------*/
- /* Define our current script functions for use in our dispatch table. */
- /*--------------------------------------------------------------------------*/
-
- int script_baud(); /* Set our baud rate to that of remote */
- int script_xmit(); /* transmit characters out the port */
- int script_pattern(); /* define a pattern to wait for */
- int script_wait(); /* wait for a pattern or timeout */
- int script_dial(); /* dial the whole number at once */
- int script_areacode(); /* transmit the areacode out the port */
- int script_phone(); /* transmit the phone number */
- int script_carrier(); /* Test point, must have carrier now */
- int script_session(); /* Exit script "successfully" */
- int script_if(); /* Branch based on pattern match */
- int script_goto(); /* Absolute branch */
-
- struct dispatch {
- char *string;
- int (*fun)();
- };
-
- struct dispatch disp_table[] = {
- { "baud", script_baud },
- { "xmit", script_xmit },
- { "pattern", script_pattern },
- { "wait", script_wait },
- { "dial", script_dial },
- { "areacode", script_areacode },
- { "phone", script_phone },
- { "carrier", script_carrier },
- { "session", script_session },
- { "if", script_if },
- { "goto", script_goto },
- { NULL, NULL }
- };
-
- char *script_dial_string = NULL; /* string for 'dial' */
- char *script_phone_string = NULL; /* string for 'phone' */
- char *script_areacode_string = " "; /* string for 'areacode' */
-
- #define PATTERNS 9
- #define PATSIZE 22
- char pattern[PATTERNS][PATSIZE]; /* 'wait' patterns */
- int scr_session_flag = 0; /* set by "session". */
- int pat_matched = -1;
- char *script_function_argument; /* argument for functions*/
-
- #define MAX_LABELS 50
- #define MAX_LAB_LEN 20
- struct lab {
- char name[MAX_LAB_LEN + 1];
- long foffset;
- int line;
- } labels[MAX_LABELS];
-
- static long offset;
- static int num_labels = 0;
- static int curline;
- static FILE *stream;
-
- extern char *BINKpath; /* path to Bink variables*/
- extern int autobaud;
-
- static char temp[256];
-
- do_script(phone_number)
- char *phone_number;
- {
- register int i,j,k;
- register char *c,*f;
- char s[64], *t;
-
- /*--------------------------------------------------------------------------*/
- /* Reset everything from possible previous use of this function. */
- /*--------------------------------------------------------------------------*/
-
- curline = 0;
- pat_matched = -1;
- num_labels = 0;
- *script_areacode_string = '\0'; /* reset the special strings */
- script_dial_string = script_phone_string = NULL;
- for (i = 0; i < PATTERNS; i++)
- {
- pattern[i][0] = 1;
- pattern[i][1] = '\0'; /* and the 'wait' patterns */
- }
- scr_session_flag = 0;
-
- /*--------------------------------------------------------------------------*/
- /* Now start doing things with phone number: */
- /* 1) construct the name of the script file into temp */
- /* 2) build script_dial_string, script_areacode_string and */
- /* script_phone_string */
- /*--------------------------------------------------------------------------*/
-
- strcpy(temp,BINKpath); /* get our current path */
- t = c = &temp[strlen(temp)]; /* point past paths */
- f = phone_number; /* then get input side */
- while (*++f != '\"') /* look for end of string */
- {
- if ((*c++ = *f) == '\0') /* if premature ending, */
- return(0);
- }
- *c = '\0'; /* Now we have the file name */
- strcpy (s, t);
-
- script_dial_string = ++f; /* dial string is rest of it */
-
- /* Say what we are doing */
- status_line (":Dialing %s with script \"%s\"", script_dial_string, s);
-
- c = script_areacode_string; /* point to area code */
- for (i = 0; i < 3, *f != '\0', *f != '-'; i++)
- *c++ = *f++; /* copy it for 'areacode' */
- *c = '\0'; /* terminate areacode */
- if (*f++ == '-')
- script_phone_string = f; /* point to phone string */
-
- /*--------------------------------------------------------------------------*/
- /* Finally open the script file and start doing some WORK. */
- /*--------------------------------------------------------------------------*/
-
- if ((stream = fopen(temp,"r")) == NULL) /* OK, let's open the file */
- {
- status_line ("!Could not open script %s", temp);
- return(0); /* no file, no work to do */
- }
-
- k = 0; /* default return is "fail" */
- while(nextline(NULL)) /* Now we parse the file ... */
- {
- k = 0; /* default return is "fail" */
- for (j = 0; (c = disp_table[j].string) != NULL; j++)
- {
- i = strlen(c);
- if (strnicmp(temp,c,i) == 0)
- {
- script_function_argument = temp + i + 1;
- if (un_attended && fullscreen)
- {
- gotoxy (0,18);
- }
- k = (*disp_table[j].fun)();
- break;
- }
- }
- if (!k || scr_session_flag) /* get out for failure or */
- break; /* 'session'. */
-
- }
- fclose(stream); /* close input file */
- if (!k)
- status_line ("+Script \"%s\" failed at line %d", s, curline);
- return(k); /* return last success/fail */
- }
-
- script_xmit()
- {
- mdm_cmd_string(script_function_argument,1);
- return(1);
- }
-
- script_areacode()
- {
- mdm_cmd_string(script_areacode_string,0);
- return(1);
- }
-
- script_phone()
- {
- mdm_cmd_string(script_phone_string,0);
- return(1);
- }
-
- script_dial()
- {
- long t,timerset();
- mdm_cmd_string(script_dial_string,0);
- mdm_cmd_char(CR); /* terminate the string */
- if (modem_response(7500)) /* we got a good response, */
- {
- timer(20); /* wait for other side */
- return(1); /* Carrier should be on now */
- }
- return(0); /* no good */
- }
-
- script_carrier()
- {
- return(CARRIER);
- }
-
- script_session()
- {
- ++scr_session_flag; /* signal end of script */
- return(1);
- }
-
- script_pattern()
- {
- register int i,j;
- register char *c;
-
- c = script_function_argument; /* copy the pointer */
- i = atoi(c); /* get pattern number */
- if (i < 0 || i >= PATTERNS) /* check bounds */
- return(0);
- c += 2; /* skip digit and space */
- for (j = 1; j <= PATSIZE, *c != '\0'; j++)
- pattern[i][j] = *c++; /* store the pattern */
- pattern[i][j] = '\0'; /* terminate it here */
- return(1);
- }
-
- script_wait()
- {
- long t1,timerset();
- register int i,j;
- register char c;
- int wait;
- int got_it = 0;
-
- pat_matched = -1;
- wait = 100 * atoi(script_function_argument); /* try to get wait length */
- if (!wait)
- wait = 4000; /* default is 40 seconds */
- t1 = timerset(wait);
- cprintf ("\r\n\033[K");
- while (!timeup(t1) && !KEYPRESS())
- {
- if (!CHAR_AVAIL()) /* if nothing ready yet, */
- {
- time_release(); /* give others a shot */
- continue; /* just process timeouts */
- }
- t1 = timerset(wait); /* reset the timeout */
- c = MODEM_IN(); /* get a character */
- if (!c) continue; /* ignore null characters */
- if (c >= ' ')
- {
- WRITE_ANSI(c&0x7f);
- }
- for (i = 0; i < PATTERNS; i++)
- {
- j = pattern[i][0]; /* points to next match char */
- if (c == pattern[i][j]) /* if it matches, */
- {
- ++j; /* bump the pointer */
- pattern[i][0] = j; /* store it */
- if (!pattern[i][j]) /* if at the end of pattern, */
- {
- ++got_it;
- pat_matched = i;
- goto done;
- }
- }
- else
- {
- pattern[i][0] = 1; /* back to start of string */
- }
- }
- }
- done:
- for (i = 0; i < PATTERNS; i++)
- {
- pattern[i][0] = 1; /* reset these for next time */
- }
- return(got_it);
- }
-
- script_baud()
- {
- int a, b;
-
- if ((b = atoi (script_function_argument)) != 0)
- {
- a = autobaud;
- autobaud = 0;
- set_baud (b, 0);
- autobaud = a;
- }
- return(1);
- }
-
- script_goto ()
- {
- int i;
-
- /* First see if we already found this guy */
- for (i = 0; i < num_labels; i++)
- {
- if (stricmp (script_function_argument, labels[i].name) == 0)
- {
- /* We found it */
- fseek (stream, labels[i].foffset, SEEK_SET);
- curline = labels[i].line;
- return (1);
- }
- }
-
- return (nextline (script_function_argument));
- }
-
- script_if ()
- {
- /* Can we go away real quick? */
- if (atoi (script_function_argument) != pat_matched)
- return (1);
-
- /* Skip the pattern number and the space */
- script_function_argument += 2;
-
- return (script_goto ());
- }
-
- nextline (str)
- char *str;
- {
- char save[256];
-
- if (str != NULL)
- strcpy (save, str);
- else
- save[0] = '\0';
-
- while(get_line ()) /* Now we parse the file ... */
- {
- if (!isalpha(temp[0]))
- {
- if (temp[0] != ':')
- {
- /* This line is a comment line */
- continue;
- }
- else
- {
- /* It is a label */
- if (num_labels >= MAX_LABELS)
- {
- status_line ("!Too many labels in script");
- return (0);
- }
- strcpy (labels[num_labels].name, &(temp[1]));
- labels[num_labels].foffset = offset;
- labels[num_labels].line = curline;
- ++num_labels;
-
- if (stricmp (&temp[1], save))
- {
- continue;
- }
- else
- {
- return (1);
- }
- }
- }
-
- if (!save[0])
- return (1);
- }
-
- return (0);
- }
-
- get_line ()
- {
- if (fgets (temp, 255, stream) == NULL)
- return (0);
-
- ++curline;
- temp[strlen(temp)-1] = '\0';
- cprintf ("\r\nScript Line %03d: %-62.62s", curline, temp);
- offset = ftell (stream);
- return (1);
- }